package com.heima.user.filter;

import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@Order(1)
@WebFilter(value = "appTokenFilter", urlPatterns = "/*")
@Component
public class AppTokenFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1.获取请求对象
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //2.查看header中是否有userId属性
        String userId = request.getHeader("userId");   //如果是设备登录,id为0
//3.如果userId有值存入到TheadLocal中
        if (StringUtils.isNotBlank(userId) && Integer.valueOf(userId) != 0) {
            ApUser apUser = new ApUser();
            apUser.setId(Integer.valueOf(userId));
            AppThreadLocalUtils.setUser(apUser);
        }
        //4.放行
        filterChain.doFilter(servletRequest, servletResponse);
        //5.清理登录信息
        AppThreadLocalUtils.clear();
    }
}
